<?php
namespace Show\Controller;
use Think\Controller;

class ScoreController extends Controller{
    /**
     * 展示排行榜
     */
    public function showScoreList()
    {
        $ip = I('get.')['ip'];
        $d['now_ip'] = $ip;

        $d['data'] = M('score')->order('max_score desc')->limit(5)->select();
        $d['count'] = M('score')->count();

        $this->assign('d',$d);
        $this->display();
    }

    /**
     * 查询个人排名
     */
    public function ranking()
    {
        $ip = I('get.')['now_ip'];
        if(!$ip){
            $this->ajaxReturn(['code'=>400,'msg'=>'请提交ip']);
        }
        $data = M('score')->field('id,ip,max_score')->where('ip',$ip)->find();
        if($data){
            $num = 0;
            $datas = M('score')->field('id,ip,max_score')->order('max_score desc')->select();
            for($i=0; $i<count($datas); $i++){
                if($data['id'] == $datas[$i]['id']){
                    $num = $i+1;
                    break;
                }
            }
            $this->ajaxReturn(['code'=>200,'ip'=>$data['ip'],'num'=>$num,'msg'=>'ok']);
        }
        else{
            $this->ajaxReturn(['code'=>500,'ip'=>$ip,'num'=>'null','msg'=>'数据库查询不到该ip']);
        }
    }

}